//
//  URLTest.h
//  sad
//
//  Created by Christopher Kemsley on 2/9/12.
//  Copyright (c) 2012 iAppolopolis. All rights reserved.
//

#ifndef sad_URLTest_h
#define sad_URLTest_h

#include "URL.hpp"
#include <iostream>



class	URLTest
{
public:
	
	static void	run ()
		{
		std::cout << "***** Beginning URL Tests *****\n" ;
		
		run ( "file://localhost/Temporary/path/file.txt" ,
			 "/Temporary/path/file.txt" ,
			 "file" ,
			 "file.txt" ,
			 "localhost" ,
			 "/Temporary/path/" ,
			 "file://localhost/Temporary/path/file.txt" ,
			 false ,
			 true ) ;
		run ( "file:/Temporary/path/file.txt" ,
			 "/Temporary/path/file.txt" ,
			 "file" ,
			 "file.txt" ,
			 "localhost" ,
			 "/Temporary/path/" ,
			 "file://localhost/Temporary/path/file.txt" ,
			 false ,
			 true ) ;
		run ( "file:Temporary/path/file.txt" ,
			 "Temporary/path/file.txt" ,
			 "file" ,
			 "file.txt" ,
			 "localhost" ,
			 "Temporary/path/" ,
			 "file:Temporary/path/file.txt" ,
			 false ,
			 false ) ;
		run ( "Temporary/path/file.txt" ,
			 "Temporary/path/file.txt" ,
			 "file" ,
			 "file.txt" ,
			 "localhost" ,
			 "Temporary/path/" ,
			 "file:Temporary/path/file.txt" ,
			 false ,
			 false ) ;
		run ( "Temporary/path/" ,
			 "Temporary/path/" ,
			 "file" ,
			 "" ,
			 "localhost" ,
			 "Temporary/path/" ,
			 "file:Temporary/path/" ,
			 true ,
			 false ) ;
		run ( "/Temporary/path/" ,
			 "/Temporary/path/" ,
			 "file" ,
			 "" ,
			 "localhost" ,
			 "/Temporary/path/" ,
			 "file://localhost/Temporary/path/" ,
			 true ,
			 true ) ;
		run ( "/" ,
			 "/" ,
			 "file" ,
			 "" ,
			 "localhost" ,
			 "/" ,
			 "file://localhost/" ,
			 true ,
			 true ) ;
		run ( "./" ,
			 "./" ,
			 "file" ,
			 "" ,
			 "localhost" ,
			 "./" ,
			 "file:./" ,
			 true ,
			 false ) ;
		run ( "http://www.apple.com/path/file.txt" ,
			 "/path/file.txt" ,
			 "http" ,
			 "file.txt" ,
			 "www.apple.com" ,
			 "/path/" ,
			 "http://www.apple.com/path/file.txt" ,
			 false ,
			 true ) ;
		run ( "http://www.apple.com/" ,
			 "/" ,
			 "http" ,
			 "" ,
			 "www.apple.com" ,
			 "/" ,
			 "http://www.apple.com/" ,
			 true ,
			 true ) ;
		run ( "http://www.apple.com" ,
			 "/" ,
			 "http" ,
			 "" ,
			 "www.apple.com" ,
			 "/" ,
			 "http://www.apple.com/" ,
			 true ,
			 true ) ;
		run ( "http:/Content/Resources/image.png" ,
			 "/Content/Resources/image.png" ,
			 "http" ,
			 "image.png" ,
			 "localhost" ,
			 "/Content/Resources/" ,
			 "http://localhost/Content/Resources/image.png" ,
			 false ,
			 true ) ;
		
		run2 ( "http://www.apple.com" ,
			  "resources/image.jpg" ,
			  "/resources/image.jpg" ,
			  false ,
			  true ) ;
		
		run2 ( "http:" ,
			  "resources/image.jpg" ,
			  "resources/image.jpg" ,
			  false ,
			  false ) ;
		
		run2 ( "http://www.apple.com/Resource/test.txt" ,
			  "resources/image.jpg" ,
			  "/Resource/resources/image.jpg" ,
			  false ,
			  true ) ;
		
		std::cout << "***** Ending URL Tests *****\n\n" ;
		}
	
	
	static void	run		(
						 char const * iFull ,
						 char const * ePath ,
						 char const * eScheme ,
						 char const * eName ,
						 char const * eHost ,
						 char const * eDirPath ,
						 char const * eString ,
						 bool isDirectory ,
						 bool isAbsolute
						 )
		{
		std::cout << "Testing: " << iFull << "\n" << std::flush ;
		
		URL		url		( iFull ) ;
		
		if ( url.path() != ePath )
			{
			std::cout << "	Path failed.\n" ;
			std::cout << "		Was: " << url.path() << "\n" ;
			std::cout << "		Expected: " << ePath << "\n" ;
			}
		
		if ( url.scheme() != eScheme )
			{
			std::cout << "	Scheme failed.\n" ;
			std::cout << "		Was: " << url.scheme() << "\n" ;
			std::cout << "		Expected: " << eScheme << "\n" ;
			}
		
		if ( url.filename() != eName )
			{
			std::cout << "	Filename failed.\n" ;
			std::cout << "		Was: " << url.filename() << "\n" ;
			std::cout << "		Expected: " << eName << "\n" ;
			}
		
		if ( url.string() != eString )
			{
			std::cout << "	String failed.\n" ;
			std::cout << "		Was: " << url.string() << "\n" ;
			std::cout << "		Expected: " << eString << "\n" ;
			}
		
		if ( url.host() != eHost )
			{
			std::cout << "	Host failed.\n" ;
			std::cout << "		Was: " << url.host() << "\n" ;
			std::cout << "		Expected: " << eHost << "\n" ;
			}
		
		if ( url.directoryURL().path() != eDirPath )
			{
			std::cout << "	directoryURL.path failed.\n" ;
			std::cout << "		Was: " << url.directoryURL().path() << "\n" ;
			std::cout << "		Expected: " << eDirPath << "\n" ;
			}
		
		if ( url.isDirectory() != isDirectory )
			{
			std::cout << "	isDirectory failed.\n" ;
			std::cout << "		Was: " << url.isDirectory() << "\n" ;
			std::cout << "		Expected: " << isDirectory << "\n" ;
			}
		
		if ( url.isAbsolute() != isAbsolute )
			{
			std::cout << "	isAbsolute failed.\n" ;
			std::cout << "		Was: " << url.isAbsolute() << "\n" ;
			std::cout << "		Expected: " << isAbsolute << "\n" ;
			}
		
		std::cout << "	Done\n" << std::flush ;
		}
	
	
	static void	run2	(
						 char const * iBase ,
						 char const * iPath ,
						 char const * ePath ,
						 bool isDirectory ,
						 bool isAbsolute
						 )
		{
		std::cout << "Testing: " << iBase << " + " << iPath << "\n" << std::flush ;
		
		URL	base	( iBase ) ;
		URL	full	( base , iPath ) ;
		
		if ( full.path().size() == 0 )
			std::cout << "	Base's path length is 0.\n" ;
		
		if ( full.scheme().size() == 0 )
			std::cout << "	Base's scheme length is 0.\n" ;
		
		if ( full.host().size() == 0 )
			std::cout << "	Base's host length is 0.\n" ;
		
		if ( full.path() != ePath )
			{
			std::cout << "	combined path failed.\n" ;
			std::cout << "		Was: " << full.path() << "\n" ;
			std::cout << "		Expected: " << ePath << "\n" ;
			}
		
		if ( full.isDirectory() != isDirectory )
			{
			std::cout << "	isDirectory failed.\n" ;
			std::cout << "		Was: " << full.isDirectory() << "\n" ;
			std::cout << "		Expected: " << isDirectory << "\n" ;
			}
		
		if ( full.isAbsolute() != isAbsolute )
			{
			std::cout << "	isAbsolute failed.\n" ;
			std::cout << "		Was: " << full.isAbsolute() << "\n" ;
			std::cout << "		Expected: " << isAbsolute << "\n" ;
			}
		
		std::cout << "	Done\n" << std::flush ;
		}
} ;



#endif
